<template>
	<view class="gather">
		<view class="gather__title">
			<input type="text" placeholder="请输入合集名称" placeholder-style="color: #aaa;" maxlength="15" v-model="title">
		</view>
		<view class="gather__content">
			<textarea name="" id="" cols="30" rows="10" v-model="brief" placeholder="请输入合集简介" placeholder-style="font-size: 25rpx;color: #aaa;" maxlength="80"></textarea>
		</view>
		<view class="gather__photo" @click="chooseImg">
			<view class="gather__photo--none" v-if="!image.img">
				<view class="gather__photo--none-icon">
					<image src="@/static/theme/default/home/xj.png" mode=""></image>
					
				</view>
				<view class="gather__photo--none-text">添加封面</view>
			</view>
			<view class="gather__photo--box" v-else>
				<image :src="image.img" mode=""></image>
				<view class="gather__photo--box-mask">修改封面</view>
			</view>
		</view>
	</view>
</template>

<script>
	import _custom from '@/common/_custom.js'
	import _data from '@/common/_data.js'
	import {sendBackFunction,uploadImages,formatImg,imageStrigify} from '@/common/custom_util.js';
	
	export default {
		data() {
			return {
				obsusername: _data.localData('name'),
				pxid: _data.localData('userid'),
				title: '',
				brief: '',
				image: {},
				data: {},
				type: 'add',
			}
		},
		onLoad(options) {
			this.data = options.data ? JSON.parse(options.data) : {};
			if(Object.keys(this.data).length) {
				this.type = 'edit';
				this.title = this.data.SeriesTitle;
				this.brief = this.data.SeriesBrief;
				this.image.img = formatImg(this.data.SeriesImage);
			}
		},
		onNavigationBarButtonTap() {
			let title = this.title.trim();
			if(!title) {
				uni.showToast({
					title: '合集名称不能为空',
					icon: 'none'
				})
				return
			}
			switch(this.type) {
				case 'add':
					this.addSubjectSeries();
					break
				case 'edit':
					this.updateSubjectSeriesById();
			}
		},
		methods: {
			/**
			 * 添加合集
			 */
			addSubjectSeries() {
				let that = this;
				_custom.getDataJava({
					apiname: 'addSubjectSeries',
					code: 3,
					apiparam: {
						createuser: this.obsusername,
						createpxid: this.pxid,
						seriestitle: this.title,
						seriesbrief: this.brief,
						seriesimage: this.image.path
					},
					success(res) {
						uni.showToast({
							title: '创建合集成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						},300)
					},
					fail() {
						uni.showToast({
							title: '创建合集失败!',
							icon: 'none'
						})
					}
				})
			},
			/**
			 * 修改合集
			 */
			updateSubjectSeriesById() {
				let that = this;
				_custom.getDataJava({
					apiname: 'updateSubjectSeriesById',
					code: 2,
					apiparam: {
						id: this.data.Id,
						seriestitle: this.title,
						seriesbrief: this.brief,
						seriesimage: imageStrigify(this.image.img)
					},
					success(res) {
						uni.showToast({
							title: '修改合集成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						},300)
					},
					fail() {
						uni.showToast({
							title: '修改合集失败!',
							icon: 'none'
						})
					}
				})
			},
			/**
			 * 点击添加封面
			 */
			chooseImg() {
				uploadImages({count: 1,filename: 'series'},res => {
					this.image = res.length ? res[0] : {};
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.gather {
		background-color: #fff;
		padding-bottom: 50rpx;
		
		&__title {
			padding: 0 20rpx;
			box-sizing: border-box;
			
			input {
				height: 100rpx;
				border-bottom: 1rpx solid #eee;
			}
		}
		
		&__content {
			padding: 0 20rpx;
			box-sizing: border-box;
			margin-top: 40rpx;
			
			textarea {
				width: 100%;
				border-bottom: 1rpx solid #eee;
			}
		}
		
		&__photo {
			width: 200rpx;
			height: 200rpx;
			background-color: #eee;
			margin-top: 40rpx;
			margin-left: 20rpx;
			border-radius: 10rpx;
			
			&--none {
				width: 100%;
				height: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				
				&-icon {
					width: 100rpx;
					height: 100rpx;
					
					image {
						width: 100%;
						height: 100%;
					}
				}
				
				&-text {
					font-size: 28rpx;
					color: #aaa;
				}
			}
			
			&--box {
				width: 100%;
				height: 100%;
				position: relative;
				
				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
				
				&-mask {
					position: absolute;
					left: 0;
					right: 0;
					bottom: 0;
					height: 60rpx;
					background-color: rgba(0, 0, 0, .6);
					font-size: 30rpx;
					color: #fff;
					text-align: center;
					line-height: 60rpx;
					border-radius: 0 0 10rpx 10rpx;
				}
			}
		}
	}
</style>
